package uy.edu.um.ltic.client.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import org.omg.CORBA.portable.ApplicationException;

import uy.edu.um.ltic.commons.EmployeeVO;

public class MyCellRenderer2 extends JLabel implements ListCellRenderer<EmployeeVO> {
	 
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public MyCellRenderer2() {
	    setOpaque(true);
	}
	
	public Component getListCellRendererComponent(JList<? extends EmployeeVO> list, EmployeeVO empleado, int index,
        boolean isSelected, boolean cellHasFocus) {
		
	
          
        String nombreEmpleado = empleado.toString();
        if (empleado.getImage()!=null){
        	
        	ImageIcon imageIcon = null;
        	
			//	imageIcon = new ImageIcon(scale(empleado.getImage(),50,50));
			imageIcon = new ImageIcon(empleado.getImage());
		   
            setIcon(imageIcon);
            Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
            setBorder(loweredetched);
        }
        
        setText("  "+nombreEmpleado+" "+empleado.getSurname()+", "+empleado.getPositionInCompany());
        
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        } 
         
        
        return this;
      
	}
/*	 private byte[] scale(byte[] fileData, int width, int height) throws
	 ApplicationException { ByteArrayInputStream in = new
	 ByteArrayInputStream(fileData); try { BufferedImage img =
	 ImageIO.read(in); if(height == 0) { height = (width * img.getHeight())/
	 img.getWidth(); } if(width == 0) { width = (height * img.getWidth())/
	 img.getHeight(); } Image scaledImage = img.getScaledInstance(width,
	 height, Image.SCALE_SMOOTH); BufferedImage imageBuff = new
	 BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	 imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0,0,0),
	 null);
	 
	 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
	 
	 ImageIO.write(imageBuff, "jpg", buffer);
	 
	 return buffer.toByteArray(); } catch (IOException e) { throw new
	 ApplicationException("jdsa", null); } }*/
    
}
